﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;
using System.Collections;
using DTO;
using BUS;

namespace QLKS
{
    public partial class frmLapHoaDon : Form
    {
        public frmLapHoaDon()
        {
            InitializeComponent();
        }

        private void txtTenKhachHang_TextChanged(object sender, EventArgs e)
        {
            if (txtTenKhachHang.Text != "")
            {
                cbbMaKhachHang.Items.Clear();
                cbbMaKhachHang.Text = "Chọn khách thanh toán";
                DataTable dt1 = KhachHangBUS.MaKhachTheoTen(txtTenKhachHang.Text);
                foreach (DataRow dr1 in dt1.Rows)
                {
                    cbbMaKhachHang.Items.Add(dr1[0]);
                }
            }
        }

        private void cbbMaKhachHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            int ma = int.Parse(cbbMaKhachHang.Text);
            DataTable dt = KhachHangDAO.KhachTheoMa(ma);
            DataRow dr = dt.Rows[0];

            List<string> lb = new List<string>();
            lb.Add("Họ tên: " + dr["TenKhachHang"].ToString());
            lb.Add("Địa chỉ: " + dr["DiaChi"].ToString());
            lb.Add("CMND: " + dr["CMND"].ToString());
            lb.Add("Loại khách: " + dr["TenLoaiKhach"].ToString());

            listBox1.DataSource = lb;

            txtTenKhachHang.Text = dr["TenKhachHang"].ToString();
            cbbMaKhachHang.Text = ma.ToString();
        }
        private void LoadListview()
        {
            listView1.Items.Clear();

            DataTable dt = PhieuThuePhongDAO.PhieuThueChuaThanhToan();

            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem lv = new ListViewItem(dr["MaPhieuThue"].ToString());

                for (int i = 1; i < dt.Columns.Count; i++)
                {
                    lv.SubItems.Add(dr[i].ToString());
                }
                listView1.Items.Add(lv);
            }
        }

        private void btnLapHoaDon_Load(object sender, EventArgs e)
        {
            LoadListview();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
             ArrayList arr = new ArrayList();
            for (int i = 0; i < listView1.SelectedItems.Count; i++)
            {
                arr.Add(listView1.Items[i].SubItems[0].Text);
            }

            float tongtien = 0;

            if (listView1.SelectedItems.Count > 1)
            {
                
                for (int j = 0; j < arr.Count; j++)
                {
                    int mapt = int.Parse(arr[j].ToString());

                    DataTable dt1 = PhieuThuePhongDAO.ThanhTienTheoMa(mapt);
                    float tien = float.Parse(dt1.Rows[0].ItemArray[0].ToString());
                    tongtien = tongtien + tien;
                }
                txtTriGia.Text = tongtien.ToString();
            }
            if (listView1.SelectedIndices.Count == 1)
            {
                int index = listView1.SelectedIndices[0];
                int mapt = int.Parse(listView1.Items[index].SubItems[0].Text);
                DataTable dt1 = PhieuThuePhongDAO.ThanhTienTheoMa(mapt);
                tongtien = float.Parse(dt1.Rows[0].ItemArray[0].ToString());
                txtTriGia.Text = tongtien.ToString();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLapHoaDon_Click(object sender, EventArgs e)
        {
            if (txtTenKhachHang.Text == "")
            {
                MessageBox.Show("Chọn khách hàng thanh toán", "Thông báo");
                return;
            }
            if (cbbMaKhachHang.SelectedIndex < 0)
            {
                MessageBox.Show("Mã khách hang không hợp lệ", "Thông báo");
                return;
            }

            if (listView1.SelectedIndices.Count > 0)
            {
                ArrayList arr = new ArrayList();
                foreach (ListViewItem item in listView1.SelectedItems)
                {
                    int ma = int.Parse(item.SubItems[0].Text);
                    arr.Add(ma);
                }

                DataTable dt = HoaDonDAO.GetAll();
                HoaDonDTO hd = new HoaDonDTO();
                hd.MaKhachHang = int.Parse(cbbMaKhachHang.Text);
                hd.NgayLap = DateTime.Now;
                hd.TriGia = 0;
                HoaDonBUS.InsertHoaDon(hd);
                float tongtien = 0;
                DataTable dt4 = HoaDonBUS.LayMaxMaHoaDon();
                for (int j = 0; j < arr.Count; j++)
                {
                    hd.MaHoaDon = int.Parse(dt4.Rows[0].ItemArray[0].ToString());
                    int mapt = int.Parse(arr[j].ToString());
                    PhieuThuePhongDAO.CapNhatPhieuThue(mapt, hd.MaHoaDon);
                    DataTable dt1 = PhieuThuePhongDAO.ThanhTienTheoMa(mapt);
                    float tien = float.Parse(dt1.Rows[0].ItemArray[0].ToString());
                    tongtien = tongtien + tien;

                    DataTable dt2 = PhieuThuePhongDAO.PhongThueTheoMa(mapt);
                    int maphong = int.Parse(dt2.Rows[0].ItemArray[0].ToString());
                    PhongDAO.UpdateTinhTrangPhong(maphong, 1);
                }

                if (MessageBox.Show("Bạn có chắc muốn lập hoá đơn này?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    HoaDonDAO.CapNhatHoaDon(tongtien, hd.MaHoaDon); ///can store
                    MessageBox.Show("Lập hóa đơn thành công", "Thông báo");
                }
                else
                {
                    return;
                }

                LoadListview();
            }
        }
    }
}
